function formatTime(number, format) {
  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 超链接跳转
 */
function handleHref(content) {
  if (!content) {
    return false;
  }
  try {
    if (content.indexOf('http://') == 0 || content.indexOf('https://') == 0) { //超链接跳转
      wx.navigateTo({
        url: '/pages/web/web?url=' +decodeURIComponent(content),
      })
      return true;
    } else if (content.indexOf('good://') == 0) { //跳转商品详情
      var data = content.substring(7, content.length)
      wx.navigateTo({
        url: '/pages/goods/goods-detail/goods-detail?' + data,
      })
      return true;
    } else if (content.indexOf("tel://") == 0) { //拨打电话
      var data = content.substring(6, content.length)
      wx.makePhoneCall({
        phoneNumber: data,
      })
      return true;
    } else if (content.indexOf("order://") == 0) { //订单详情
      var data = content.substring(8, content.length)
      wx.navigateTo({
        url: '/pages/order/order-detail/order-detail?' + data,
      })
      return true;
    }
  } catch (e) {}

  return false
}

function handleShareApp(page, res) {
  if (res) {
    return {
      title: res.goodName || '丰产会',
      path: '/pages/splash/splash?page=' + page + "&goodId=" + res.goodId + "&skuId=" + res.skuId,
      imageUrl: res.thumbUrl || '/resources/image/share_img.png'
    }
  } else {
    return {
      title: '丰产会',
      path: '/pages/splash/splash',
      imageUrl: '/resources/image/share_img.png'
    }
  }

}

module.exports = {
  formatTime: formatTime,
  handleHref: handleHref,
  handleShareApp: handleShareApp
}